<?php defined('SYSPATH') or die('No direct script access.');

class Model_Services_Selectedsegment extends Model{
	
	public $selected_segments = array();
		
	public function __construct($analysis_id=false){
		if ($analysis_id) $this->selected_segments = $this->get($analysis_id);
	}
	
	public function get($id){
		return ORM::factory("dao_selectedsegments")->where("analysis_id","=",$id)->order_by("number","asc")->find_all()->as_array();
	}
	
	public function getSelectedSegment($selected_segment_id = false){
		if ($selected_segment_id) return ORM::factory("dao_selectedsegments")->find($selected_segment_id);
		return $this->selected_segments;	
	}
	
	public function getSegmentData($dataset){ return Model_Segment::create($dataset);}

	//Grabo de a un segmento seleccionado por vez
	public function save($id, array $data){
		
		$daoSelectedSegments = ($id > 0) ? ORM::factory("dao_selectedsegments",$id) : ORM::factory("dao_selectedsegments");
		
		$daoSelectedSegments->number = intval($data["number"]);
		$daoSelectedSegments->segment = $data["segment"];
		$daoSelectedSegments->analysis_id = $data["analysis_id"];
		
		$daoSelectedSegments->save();
		
		return $daoSelectedSegments->id;
		
	}
	
	public function delete($id){
		$daoSelected = ORM::factory("dao_selectedsegments",$id);
		$daoSelected->delete();
		return $id;
	}
	
	
}
?>